package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.g2room.driving.util.DbUtil;
import com.g2room.driving.util.HtmlUtil;
import com.g2room.driving.util.IpUtil;
import com.gargoylesoftware.htmlunit.WebClient;

public class GenericGrap {
	public static String[] uas = new String[]{
		"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
		"Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50",
		"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 ",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
		"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
		"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
		"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
		"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400",
		"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E) ",
		"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0) ",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36",
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36"
	};
	
	public static WebClient getWebClient() {
		 //创建一个webclient
		WebClient wc = new WebClient();
		String ip = IpUtil.getIp();
		wc.addRequestHeader("X-Real-IP", ip);
		wc.addRequestHeader("x-forwarded-for", ip);
		wc.addRequestHeader("WL-Proxy-Client-IP", ip);
		////设置请求报文头里的User-Agent字段
      wc.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
      //wc.addRequestHeader和request.setAdditionalHeader功能应该是一样的。选择一个即可。
      //其他报文头字段可以根据需要添加
      wc.getCookieManager().setCookiesEnabled(false);//开启cookie管理
      wc.getOptions().setJavaScriptEnabled(true);//开启js解析。对于变态网页，这个是必须的
      wc.getOptions().setCssEnabled(true);//开启css解析。对于变态网页，这个是必须的。
      wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
      wc.getOptions().setThrowExceptionOnScriptError(false);
      wc.getOptions().setTimeout(10000);
      
      //htmlunit 对css和javascript的支持不好，所以请关闭之
      wc.getOptions().setJavaScriptEnabled(false);
      wc.getOptions().setCssEnabled(false);
      return wc;
	}
	

	public static Document getDoc(String url) {
		return getDoc(url, "utf-8");
	}
	public static Document getDoc(String url, String code) {
//		String ip = IpUtil.getIp();
		Document doc = null;
		try {
			doc =  Jsoup.parse(new URL(url).openStream(), code, url);
//			 
//			doc =Jsoup.connect(url)
////				  .data("query", "Java")   // 请求参数
//					.header("X-Real-IP", ip)
//					.header("x-forwarded-for", ip)
//					.header("WL-Proxy-Client-IP", ip)
//					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent 
////				.cookie("auth", "token") // 设置 cookie 
//					.timeout(5000)           // 设置连接超时时间
//					.get(); // get方式访问
		} catch (IOException e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	public static Document getDocByIp(String url) throws IOException {
		String ip = IpUtil.getIp();
		Document doc = null;
		doc =Jsoup.connect(url)
//				  .data("query", "Java")   // 请求参数
				.header("X-Real-IP", ip)
				.header("x-forwarded-for", ip)
				.header("WL-Proxy-Client-IP", ip)
				.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2") // 设置 User-Agent 
//				.cookie("auth", "token") // 设置 cookie 
				.timeout(5000)           // 设置连接超时时间
				.get(); // get方式访问
		return doc;
	}
	
	public static Document getDocByHtml(String html) {
		return  Jsoup.parse(html);
	}
}
